package com.le.controllers;

import com.le.bean.Topic;
import com.le.biz.TopicBiz;
import com.le.web.model.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("topic") // http://localhost:port/topic
@Slf4j
@Api(tags = "主贴")
public class TopicController {
    @Autowired
    private TopicBiz topicBiz;

    @RequestMapping(value = "findByPage", method = {RequestMethod.GET})
    @ApiOperation(value = "分页查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageno", value = "页号", required = true),
            @ApiImplicitParam(name = "pagesize", value = "每页记录数", required = true),
            @ApiImplicitParam(name = "boardid", value = "板块号", required = true)
    })
    public Map<String, Object> findByPage(@RequestParam int pageno, @RequestParam int pagesize, @RequestParam Integer boardid){
        Map<String, Object> map = new HashMap<>(); // 返回json字符串

        PageBean<Topic> page = null;
        try {
            page = this.topicBiz.findByPage(pageno, pagesize, boardid);
        } catch (Exception e){
            map.put("code", 0);
            map.put("msg", e.getCause());
            e.printStackTrace();
            return map;
        }
        map.put("code", 1);
        map.put("data", page);
        return map;
    }

    @RequestMapping(value = "findTopicById", method = {RequestMethod.GET})
    @ApiOperation(value = "根据topicid查询topic详情操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "topicid", value = "帖子编号", required = true)
    })
    public Map<String, Object> findTopicById(@RequestParam Integer topicid) {
        Map<String, Object> map = new HashMap<>(); // 返回json字符串
        Topic t = null;
        try {
            t = this.topicBiz.findById(topicid);
        } catch (Exception e){
            e.printStackTrace();
            map.put("code", 0);
            map.put("msg", e.getCause());
            return map;
        }
        map.put("code", 1);
        map.put("data", t);
        return map;
    }

    @RequestMapping(value = "deleteTopicById", method = {RequestMethod.GET})
    @ApiOperation(value = "根据topicid删除topic的操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "topicid", value = "帖子编号", required = true)
    })
    public Map<String, Object> deleteTopicById(@RequestParam Integer topicid) {
        Map<String, Object> map = new HashMap<>(); // 返回json字符串
        this.topicBiz.deleteTopicById(topicid);
        map.put("code", 1);
        return map;
    }

    @RequestMapping(value = "addTopic", method = {RequestMethod.POST})
    @ApiOperation(value = "发布帖子的操作")
    public Map<String, Object> addTopic(Topic topic) {
        Map<String, Object> map = new HashMap<>(); // 返回json字符串
        this.topicBiz.addTopic(topic);
        map.put("code", 1);
        return map;
    }

    @RequestMapping(value = "updateTopicById", method = {RequestMethod.POST})
    @ApiOperation(value = "修改帖子的操作")
    public Map<String, Object> updateTopicById(Topic topic) {
        Map<String, Object> map = new HashMap<>(); // 返回json字符串
        this.topicBiz.updateTopic(topic);
        map.put("code", 1);
        return map;
    }
}
